package com.ukao.cashregister.dialog;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.adapter.RefundRecordAdapter;
import com.ukao.cashregister.base.BaseDialogFragment;
import com.ukao.cashregister.bean.RefundRecordBean;
import com.ukao.cashregister.widget.StateButton;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import info.hoang8f.android.segmented.SegmentedGroup;

/**
 * 退款记录
 * Created by ukaodev on 2017/6/2.
 */

public class RefundRecordDialogFragment extends BaseDialogFragment {

    Unbinder unbinder;
    @BindView(R.id.close_btn)
    ImageView closeBtn;
    @BindView(R.id.order_no)
    TextView orderNo;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.cancel)
    StateButton cancel;
    @BindView(R.id.submit)
    Button submit;
    @BindView(R.id.tab_one)
    RadioButton tabOne;
    @BindView(R.id.tab_two)
    RadioButton tabTwo;
    @BindView(R.id.segmented_group)
    SegmentedGroup segmentedGroup;
    @BindView(R.id.auditRecord_view)
    RecyclerView auditRecordView;


    private RefundRecordAdapter mRefundRecordAdapter;
    private ArrayList<RefundRecordBean.ListBean> mRefundRecordBean;
    private ArrayList<RefundRecordBean.ListBean> mAuditRecordBean;
    private String orderNO;
    private RefundRecordAdapter mAuditRecordAdapter;
    private boolean isOrderRefund;

    /**
     *
     * @param mRefundRecordBean  退款成功
     * @param mAuditRecordBean   审核记录
     * @param orderNO
     *
     * @return
     */
    public static RefundRecordDialogFragment newInstance(ArrayList<RefundRecordBean.ListBean> mRefundRecordBean,
                                                         ArrayList<RefundRecordBean.ListBean> mAuditRecordBean, String orderNO  ) {
        RefundRecordDialogFragment fragment = new RefundRecordDialogFragment();
        Bundle args = new Bundle();
        args.putParcelableArrayList(ARG_PARAM1, mRefundRecordBean);
        args.putParcelableArrayList(ARG_PARAM2, mAuditRecordBean);
        args.putString(ARG_PARAM3, orderNO);

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mRefundRecordBean = (ArrayList<RefundRecordBean.ListBean>) getArguments().getSerializable(ARG_PARAM1);
            mAuditRecordBean = (ArrayList<RefundRecordBean.ListBean>) getArguments().getSerializable(ARG_PARAM2);
            orderNO = getArguments().getString(ARG_PARAM3);

        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.refund_record_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        orderNo.setText("退款单号：" + orderNO);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        mRefundRecordAdapter = new RefundRecordAdapter(activity, mRefundRecordBean);
        mRefundRecordAdapter.setRefundRecordEnter(true);
        recyclerView.setAdapter(mRefundRecordAdapter);

        auditRecordView.setLayoutManager(new LinearLayoutManager(activity));
        mAuditRecordAdapter= new RefundRecordAdapter(activity, mAuditRecordBean);
        mAuditRecordAdapter.setRefundRecordEnter(false);
        auditRecordView.setAdapter(mAuditRecordAdapter);
        initListener();


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    protected void initListener() {
        super.initListener();
        segmentedGroup.setOnCheckedChangeListener(mOnCheckListener);
    }


    private RadioGroup.OnCheckedChangeListener mOnCheckListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            switch (checkedId) {
                case R.id.tab_one:

                    auditRecordView.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.VISIBLE);
                    break;
                case R.id.tab_two:

                    auditRecordView.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.GONE);

                    break;

                default:
            }
        }

    };


    @OnClick({R.id.close_btn, R.id.cancel, R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close_btn:
                dismiss();
                break;
            case R.id.cancel:
                dismiss();
                break;
            case R.id.submit:
                dismiss();
                break;
        }
    }
}
